<template>
    <div>
        <div style="margin-bottom: 20px;margin-left: 93%"><el-button type="primary" @click="tianjia">添加文章</el-button></div>
        <el-table
                :data="tableData"
                border
                style="width: 100%">
            <el-table-column prop="id" label="编号" width="50">
            </el-table-column>
            <el-table-column
                    prop="rqi"
                    label="日期"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="活动名称"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="title"
                    width="180"
                    label="标题">
            </el-table-column>
            <el-table-column prop="author" label="作者" width="70"></el-table-column>
            <el-table-column prop="neirongtow" label="内容" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column label="是否开始">
                <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.ztai==1">开始</el-tag>
                    <el-tag type="danger" v-else>未开始</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="150">
                <template slot-scope="scope">
                    <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
                    <el-button type="text" size="small" @click="updateContent(scope.row)">编辑</el-button>
                    <el-button type="text" size="small" @click="handleDelet(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                @current-change="handleCurrentChange"
                :current-page="currentPage4"
                layout="total,prev, pager, next"
                :total="total">
        </el-pagination>

        <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%">

            <span v-html="content">{{content}}</span>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
        </el-dialog>



        <el-dialog title="修改活动信息" :visible.sync="dialogFormVisible">
            <el-form :rules="rules" :model="form" ref="ruleForm">
                <el-form-item prop="id" label="活动编号" :label-width="formLabelWidth">
                    <el-input v-model="form.id" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="name" label="活动名称" :label-width="formLabelWidth">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="活动标题" :label-width="formLabelWidth">
                    <el-input v-model="form.title" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="作者名" :label-width="formLabelWidth">
                    <el-input v-model="form.author" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="ztai" label="是否展示" :label-width="formLabelWidth">

                    <el-radio-group v-model="form.ztai">
                        <el-radio :label="1" style="margin-top: 10px;">展示</el-radio>
                        <el-radio :label="0" style="margin-top: 10px;">不展示</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addContent('ruleForm')">确 定</el-button>
            </div>

        </el-dialog>





        <el-dialog title="添加活动信息" :visible.sync="dialogFormVisible1">
            <el-form :rules="rules" :model="form1" ref="ruleForm">

                <el-form-item prop="name" label="活动名称" :label-width="formLabelWidth">
                    <el-input v-model="form1.name" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="活动标题" :label-width="formLabelWidth">
                    <el-input v-model="form1.title" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="活动内容" :label-width="formLabelWidth">
                    <input
                            type="file"
                            ref="inputer"
                            accept="application/vnd.ms-powerpoint, application/vnd.openxmlformats-officedocument.presentationml.presentation, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/pdf"
                    >
                    <p>上传文章word文档</p>
                </el-form-item>

                <el-form-item label="作者名" :label-width="formLabelWidth">
                    <el-input v-model="form1.author" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="ztai" label="是否展示" :label-width="formLabelWidth">

                    <el-radio-group v-model="form1.ztai">
                        <el-radio :label="1" style="margin-top: 10px;">展示</el-radio>
                        <el-radio :label="0" style="margin-top: 10px;">不展示</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
                <el-button type="primary" @click="addContent1('ruleForm')">确 定</el-button>
            </div>

        </el-dialog>


    </div>
</template>

<script>
    export default {
        name: "activity",
        data(){
            return{
                total:0,
                formData: new FormData(),
                file: {}, //文件数据
                tableData: [],
                dialogVisible: false,
                content:'',
                dialogFormVisible: false,
                dialogFormVisible1:false,
                form: {
                    id:'',
                    title:'',
                    name: '',
                    author:'',
                    ztai:''
                },
                form1: {
                    title:'',
                    name: '',
                    author:'',
                    ztai:''
                },
                formLabelWidth: '120px',
                rules: {
                    id:[
                        {required: true, message: '请输入活动编号', trigger: 'blur'}

                    ],
                    name: [
                        { required: true, message: '请输入活动名称', trigger: 'blur' }
                    ],
                    name2:[
                        {required:true,message:'请输入活动名称',trigger:'blur'}
                    ],
                    region: [
                        { required: true, message: '请选择活动区域', trigger: 'change' }
                    ],
                    date1: [
                        { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                    ],
                    date2: [
                        { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
                    ],
                    type: [
                        { type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
                    ],
                    ztai: [
                        { required: true, message: '请选择活动资源', trigger: 'change' }
                    ],
                    desc: [
                        { required: true, message: '请填写活动形式', trigger: 'blur' }
                    ]
                },
                currentPage4: 1
            }
        },
        created(){
            this.init();
        },
        methods:{
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage4=val;
                this.init();
            },
            //上传文件
            addFile: function() {
                var _this = this;
                let inputDOM = this.$refs.inputer;
                // let oldLen = this.filLen;
                this.file = inputDOM.files[0];
                if(this.file==null){
                    this.$message({
                        type:'error',
                        message:'文件不能为空'
                    })
                }else{
                    let len = this.file.length;
                    this.formData.append("file", this.file);
                    this.$http({
                        method: "post",
                        url: "/activity/add",
                        data: this.formData,
                        params:this.form1,
                        headers: {
                            "Content-Type": "multipart/form-data"
                        }
                    }).then(res=>{
                        if(res.status==200&&res.data==1){
                            this.$message({
                                type:'success',
                                message:'添加成功'
                            })
                            this.init();
                        }else if(res.status==200&&res.data==0){
                            this.$message({
                                type:'error',
                                message:'添加失败'
                            })
                        }else{
                            this.$message({
                                type:'error',
                                message:'请添加word文档'
                            })
                        }
                    })
                }


            },
            init(){
                let par={
                    num:0,
                    size:10
                };
                par.num=this.currentPage4;
                this.$http({
                    url:'/activity/getPage',
                    method:'get',
                    params:par
                }).then(result=>{
                    if(result.status==200){
                        this.total=result.data.total;
                        this.tableData=result.data.list;
                        console.log(result)
                    }
                })
            },
            handleClick(row){
                console.log(row);
                this.dialogVisible=true;
                this.content=row.neirong;
                console.log(this.content);
            },
            handleDelet(row){
                this.$confirm('确认删除？')
                    .then(_ => {
                        this.$http({
                            url:'/activity/delete/'+row.id,
                            method: 'DELETE'
                        }).then(result=>{
                            console.log(result)
                            if(result.status==200&&result.data==1){
                                this.$message({
                                    type:'success',
                                    message:'删除成功'
                                })
                                this.init();
                            }
                        })
                    })
                    .catch(_ => {});
            },
            updateContent(row){
                this.form.id=row.id;
                this.form.name=row.name;
                this.form.title=row.title;
                this.form.author=row.author;
                this.form.ztai=row.ztai;
                this.dialogFormVisible=true
            },
            addContent1(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.addFile();
                        this.dialogFormVisible1 = false;
                        this.$refs[formName].resetFields();
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            addContent(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('是否修改!');
                        this.form.id=parseInt(this.form.id);
                        this.$http({
                            url:'/activity/update',
                            method:'put',
                            params:this.form
                        }).then(result=>{
                            console.log(result);
                            if(result.status==200){
                                this.$message({
                                    type:'success',
                                    message:'修改成功'
                                })
                                this.init();
                            }
                        })
                        this.dialogFormVisible = false;
                        this.$refs[formName].resetFields();
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            tianjia(){
                this.dialogFormVisible1=true;
            }
        }
    }
</script>

<style scoped>

</style>
